#coding=utf-8
from datetime import datetime
import logging
import string
import urllib
import re
from google.appengine.ext import db
from content import ContentClass, ReplyContent
from control import getTimeIndex
from models import Setting, Images
from photoLib import PhotoLib, PhotoLib_Photo
from reply import PublicPage
from google.appengine.api import urlfetch, memcache
from subscribe import Subscribe, SubscribeOther
from tool import filterNone

__author__ = 'Administrator'

class PubWeiboMsg(PublicPage):
    def post(self, *args):
        num=self.request.get('num')
        if int(num)>0:
            conlist=[]
            if  ContentClass().all().filter('code =','a33').count()==0:
                contentclass=ContentClass()
                contentclass.code='a33'
                contentclass.title='公共微博'.decode('utf-8')
                contentclass.content='公共微博'.decode('utf-8')
                contentclass.maincode='a33'
                contentclass.father=''
                contentclass.level='1'
                contentclass.status='1'
                contentclass.updateSpanTime='100'
                contentclass.put()
                replycontent=ReplyContent()
                replycontent.level='100'
                replycontent.code=contentclass.code+'-s1'
                replycontent.maincode='a33'
                replycontent.content='**公共微博**'.decode('utf-8')
                replycontent.father=contentclass.code
                replycontent.status='1'
                replycontent.replyType='11'
                replycontent.put()
            replaylist=[]
            for r in ReplyContent().all().filter('father =','a33-s1').filter('status =','1').order('-lastUpdateTime'):
                r.status='0'
                replaylist.append(r)
            for reply in replaylist[15:]:
                db.delete(filterNone(Images.get_by_id(reply.imglist)))
                piclist=PhotoLib_Photo.get_by_id(reply.photolist)
                for pic in piclist:
                    if pic:
                        pic.status='0'
                        pic.lastUpdateTime=datetime.now()
                        db.put(pic)
            db.put(replaylist[15:])
#            for r in ReplyContent().all().filter('code =','a33-s1').fetch(1):
#                r.lastUpdateTime=datetime.now()
#                r.put()
#            weibosite=self.request.get('weibosite')#官网编码：01是新浪
            recode=getTimeIndex()
            for i in range(int(num)):
                imgs=self.request.get(str(i)+'img')
                imglist=[]
                photolist=[]
                if imgs:
                    lib=getTmpLinkLib()
                    otherId=int(imgs.split(',')[0])
                    img=Images()
                    img.name=imgs.split(',')[1]
                    img.otherId=otherId
                    img.isDown=False
                    img.updataTime=datetime.now()
                    img.appname='weibo'
                    img.put()
                    img.photoCode=str(img.key().id())
                    img.put()
                    photoLib_Photo = PhotoLib_Photo()
                    photoLib_Photo.photoLibCode=str(lib.photoLibCode)
                    photoLib_Photo.photoCode=img.photoCode
                    photoLib_Photo.indexNo = string.atoi(getTimeIndex());
                    photoLib_Photo.status = '1'
                    photoLib_Photo.put()
                    imglist.append(img.key().id())
                    photolist.append(photoLib_Photo.key().id())
                code=self.request.get(str(i)+'replycode')
                userid=self.request.get(str(i)+'user')
                if ReplyContent().get_by_key_name('u'+code+'+'+userid)==None and userid:
                    #title=self.request.get(str(i)+'title')
                    content=self.request.get(str(i)+'content')
                    replycontent=ReplyContent(key_name='u'+code+'+'+userid)
                    replycontent.level='101'
                    if i>9:
                        replycontent.code='a33-s1-'+recode+str(i)
                    else:
                        replycontent.code='a33-s1-'+recode+'0'+str(i)
                    replycontent.maincode='a33'
                    if imgs:
                        replycontent.content='**'+content+'[*TmpLink/'+img.photoCode+'*]'+'**'
                    else:
                        replycontent.content='**'+content+'**'
                    replycontent.father='a33-s1'
                    replycontent.status='1'
                    replycontent.replyType='11'
                    replycontent.imglist=imglist
                    replycontent.photolist=photolist
                    conlist.append(replycontent)
            putresult=db.put_async(conlist)
            putresult.get_result()






class WeiboSubMsg(PublicPage):
    def post(self, *args):
        username=self.request.get('username')
        if username:
            slist= Subscribe().all().filter('userName =',username).filter('maincode =','a3').fetch(1)
            if not slist:
                s=Subscribe()
                s.userName=username
                s.maincode='a3'
                s.status='1'
                s.subscribeType='1'
                s.put()
            elif slist[0].status=='0':
                slist[0].status='1'
                slist[0].lastUpdateTime=datetime.now()
                slist[0].put()
#            else:
#                slist[0].lastUpdateTime=datetime.now()
#                slist[0].put()
            slist2= Subscribe().all().filter('userName =',username).filter('maincode =','a33').fetch(1)
            if not slist2:
                s=Subscribe()
                s.userName=username
                s.maincode='a33'
                s.status='1'
                s.subscribeType='0'
                s.put()
            elif slist2[0].status=='0':
                slist2[0].status='1'
                slist2[0].lastUpdateTime=datetime.now()
                slist2[0].put()
#            else:
#                slist2[0].lastUpdateTime=datetime.now()
#                slist2[0].put()
            #更新了主订阅
            #添加下级订阅
            addlist=self.request.get('add').split(',')
            addlist+=self.request.get('friends').split(',')
            addsublist=[]
            addsubcode=[]
            for a in addlist:
                if SubscribeOther().all().filter('userName =',username).filter('code =',a).count()==0 and a not in addsubcode:
                    sub=SubscribeOther()
                    sub.maincode='a3'
                    sub.code=a
                    sub.level='2'
                    sub.userName=username
                    sub.father='a3'
                    sub.subscribeType='0'
                    addsublist.append(sub)
                    addsubcode.append(a)
            db.put(addsublist)
            dellist=self.request.get('del').split(',')
            while dellist:
                db.delete(SubscribeOther().all().filter('userName =',username).filter('code in',dellist[:10]).fetch(10))
                dellist=dellist[10:]
            userweibo='a3-0'+username
            if SubscribeOther().all().filter('userName =',username).filter('code =',userweibo).count()==0:
                sub=SubscribeOther()
                sub.maincode='a3'
                sub.code=userweibo
                sub.level='2'
                sub.userName=username
                sub.father='a3'
                sub.subscribeType='0'
                sub.put()
                logging.info('a3')
                con=ContentClass()
                con.maincode='a3'
                con.code=userweibo
                con.content='我的微博'.decode('utf-8')
                con.title='我的微博'.decode('utf-8')
                con.father='a3'
                con.level='2'
                con.updateSpanTime='100'
                con.status='1'
                con.lastUpdateTime=datetime.now()
                con.put()
                logging.info('a3-0')
                replycontent=ReplyContent()
                replycontent.level='100'
                replycontent.code=userweibo+'-s'+username
                replycontent.maincode='a3'
                replycontent.content='**我的微博**'.decode('utf-8')
                replycontent.father=userweibo
                replycontent.status='1'
                replycontent.replyType='9'
                replycontent.put()
#            for r in ReplyContent().all().filter('code =',userweibo+'-s'+username).fetch(1):
#                r.lastUpdateTime=datetime.now()
#                r.put()
            replylist=[]
            replaycode=userweibo+'-s'+username
            weibosite=self.request.get('weibosite')#官网编码：01是新浪
            recode=replaycode+'-'+getTimeIndex()+weibosite
            for i in range(int(self.request.get('usernum'))):
                imgs=self.request.get(str(i)+'img')
                imglist=[]
                photolist=[]
                if imgs:
                    lib=getTmpLinkLib()
                    otherId=int(imgs.split(',')[0])
                    img=Images()
                    img.name=imgs.split(',')[1]
                    img.otherId=otherId
                    img.isDown=False
                    img.updataTime=datetime.now()
                    img.appname='weibo'
                    img.put()
                    img.photoCode=str(img.key().id())
                    img.put()
                    photoLib_Photo = PhotoLib_Photo()
                    photoLib_Photo.photoLibCode=str(lib.photoLibCode)
                    photoLib_Photo.photoCode=img.photoCode
                    photoLib_Photo.indexNo = string.atoi(getTimeIndex());
                    photoLib_Photo.status = '1'
                    photoLib_Photo.put()
                    imglist.append(img.key().id())
                    photolist.append(photoLib_Photo.key().id())
#                code=replaycode+'-'+
                content=self.request.get(str(i)+'userweibotext')
                if ReplyContent().get_by_key_name(self.request.get(str(i)+'userweiboid')+username):
                    continue
                replycontent=ReplyContent(key_name=self.request.get(str(i)+'userweiboid')+username)
                replycontent.level='101'
                if i<10:
                    replycontent.code=recode+'0'+str(i)
                else:
                    replycontent.code=recode+str(i)
                replycontent.maincode='a3'
                if imgs:
                    replycontent.content='**'+content+'[*TmpLink/'+img.photoCode+'*]'+'**'
                else:
                    replycontent.content='**'+content+'**'
                replycontent.father=replaycode
                replycontent.status='1'
                replycontent.imglist=imglist
                replycontent.photolist=photolist
                replycontent.replyType='9'
                replylist.append(replycontent)
            db.put(replylist)
            self.response.out.write('0')#操作成功


def getTmpLinkLib():#微博图库
    tmp='TmpLink'
    lib=PhotoLib().all().filter('photoLibName =',tmp).fetch(1)
    if len(lib)==0:
        lib=PhotoLib()
        lib.photoLibCode=string.atoi(getTimeIndex())
        lib.photoLibName=tmp
        lib.put()
    else:
        lib=lib[0]
    return lib

class WeiboMsg(PublicPage):
    def post(self, *args):
        fathercode='a3'
        for i in range(25):
            code=self.request.get(str(i)+'code')
            if code:
                c=ContentClass().all().filter('code =',fathercode+'-'+code).fetch(1)
                if c:
                    content=c[0]
                else:
                    content=ContentClass()
                    content.code=fathercode+'-'+code
                    content.maincode=fathercode
                    content.content=self.request.get(str(i)+'title')
                    content.father=fathercode
                    content.level='2'
                    content.updateSpanTime='100'
                    content.status='1'
                    content.lastUpdateTime=datetime.now()
                    content.put()
                rc=ReplyContent().all().filter('code =',fathercode+'-'+code+'-s'+code).fetch(1)
                if rc:
                    replay=rc[0]
                else:
                    replay=ReplyContent()
                    replay.maincode = fathercode#主编号
                    replay.code = fathercode+'-'+code+'-s'+code#编号
                    replay.father = content.code#父级
                    replay.content ='**'+self.request.get(str(i)+'title')+'**'
                    replay.replyType = '10'
                    replay.status = '1'
                    replay.level='100'
                    replay.lastUpdateTime=datetime.now()
                    replay.put()
                timecode=getTimeIndex()
                for k in range(25):
                    imgs=self.request.get(str(i)+'img'+str(k))
                    imglist=[]
                    photolist=[]
                    if imgs:
                        lib=getTmpLinkLib()
                        otherId=int(imgs.split(',')[0])
                        img=Images()
                        img.name=imgs.split(',')[1]
                        img.otherId=otherId
                        img.isDown=False
                        img.updataTime=datetime.now()
                        img.appname='weibo'
                        img.put()
                        img.photoCode=str(img.key().id())
                        img.put()
                        photoLib_Photo = PhotoLib_Photo()
                        photoLib_Photo.photoLibCode=str(lib.photoLibCode)
                        photoLib_Photo.photoCode=img.photoCode
                        photoLib_Photo.indexNo = string.atoi(getTimeIndex());
                        photoLib_Photo.status = '1'
                        photoLib_Photo.put()
                        imglist.append(img.key().id())
                        photolist.append(photoLib_Photo.key().id())
                    replaycode=self.request.get(str(i)+'replaycode'+str(k))
                    if replaycode:
                        if ReplyContent().get_by_key_name(key_names=replaycode):
                            continue
                        replyContent = ReplyContent(key_name=replaycode)
                        replyContent.maincode = fathercode#主编号
                        if k>9:
                            replyContent.code = replay.code+'-'+timecode+str(k)#编号
                        else:
                            replyContent.code = replay.code+'-'+timecode+'0'+str(k)#编号
                        replyContent.father = replay.code#父级
                        if imgs:
                            replyContent.content = '**'+self.request.get(str(i)+'content'+str(k))+'[*TmpLink/'+img.photoCode+'*]'+'**'#内容
                        else:
                            replyContent.content = '**'+self.request.get(str(i)+'content'+str(k))+'**'#内容
                        replyContent.replyType = '10'
                        replyContent.status = '1'
                        replyContent.imglist=imglist
                        replyContent.photolist=photolist
                        replyContent.level='101'
                        replyContent.put()
                    else:
                        break
            else:
                break
        self.response.out.write('0')


def updateWeibo(username):
    setting=Setting().all().fetch(1)
    if setting:
        setting=setting[0]
        if setting.weiboSite:
            login_url =setting.weiboSite+ '/getFriends_ids?username='+username
#            login_data = urllib.urlencode(pam)
            try:
                result = urlfetch.fetch(
                url = login_url,
                payload = {},
                method = urlfetch.GET,
                headers = {'Content-Type':'application/x-www-form-urlencoded',
                           'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6'},
                follow_redirects = False,deadline=2)
            except:
                logging.info('/getFriends_ids?username='+username)
        else:
            setting.weiboSite='http://127.0.0.1:8080'
            setting.put()
    else:
        web=Setting()
        web.weiboSite='http://127.0.0.1:8080'
        web.put()

def A3replay(self):
    setting=Setting().all().fetch(1)
    if not setting:
        self.response.out.write('1')
        return
    setting=setting[0]
    replaytype=self.request.get('Type')
    code=self.request.get('Code')
    username=self.request.get('UserName')
    for s in self.request.query_string.split('&'):
        if 'Content'==s[0:7] and len(s)>8:
            unquotedPath= urllib.unquote(s[8:])
#            unquotedPath = unquote(path)
            try:
                getcontent = unicode(unquotedPath, 'utf8')
            except:
                try:
                  getcontent = unicode(unquotedPath, 'gbk')
                except:
                    getcontent=''
#    getcontent=self.request.get('Content')
    dotype=''
    contenttext=''
    for s in getcontent.split('**'):
        if s:
            if dotype:
                contenttext=s
            else:
                dotype=s
    isGet=False
    pam={}
    isreplay=False
    delReplay=False
    if '9'==replaytype:
        if code.find('-0'+username+'-')!=-1:
            if '1'==dotype:
                lastre=ReplyContent().all().filter('code >','a3-0'+username+'-s'+username).filter('code <','a3-0'+username+'-s'+username+u'\ufffd').order('-code').fetch(1)
                if lastre:
                    weiboweb=lastre[0].code.split('-')[-1]
                    if lastre[0].content=='**'+contenttext+'**':
                        self.response.out.write('1')
                        return
                else:
                    weiboweb='11'
#                lastcode=int(weiboweb[:-1])+1
                lastcode=str(int(weiboweb[:-1])+1)+weiboweb[-1]
                replay=ReplyContent()
                replay.maincode = 'a3'#主编号
                replay.code = 'a3-0'+username+'-s'+username+'-'+lastcode#编号
                replay.father = 'a3-0'+username+'-s'+username#父级
                replay.content ='**'+contenttext+'**'
                replay.replyType = '9'
                replay.status = '1'
                replay.level='101'
                isreplay=True
                pam['username']=username
                pam['text']=contenttext.encode('utf-8')
                pam['code']=replay.code
                imgids=re.findall('\[\*[A-Za-z]+/(\d+)\*\]',contenttext)
                if imgids:
                    pam['img']=imgids[0]
                url= setting.weiboSite+ '/putUserWeibo'
            if '2'==dotype:
#                weiboid=code.split('-')[-1]
                realCode=ReplyContent().all().filter('code =',code).fetch(1)
                if realCode[0].key().name():
                    weiboid=realCode[0].key().name()[1:]
                else:
                    weiboid=code
                url= setting.weiboSite+ '/deleteUserWeibo?weiboid='+weiboid+'&username='+username
                isGet=True
                delReplay=ReplyContent().all().filter('code =',code).fetch(1)
                if delReplay:
                    delReplay[0].status='0'

                
    if '10'==replaytype:
        if '1'==dotype:#转发微博
            lastre=ReplyContent().all().filter('code >','a3-0'+username+'-s'+username).filter('code <','a3-0'+username+'-s'+username+u'\ufffd').order('-code').fetch(1)
            if lastre:
                weiboweb=lastre[0].code.split('-')[-1]
            else:
                weiboweb='11'
#            lastcode=int(weiboweb[:-1])+1
            lastcode=str(int(weiboweb[:-1])+1)+weiboweb[-1]
            replay=ReplyContent()
            replay.maincode = 'a3'#主编号
            replay.code = 'a3-0'+username+'-s'+username+'-'+lastcode#编号
            replay.father = 'a3-0'+username+'-s'+username#父级

            recontent=ReplyContent().all().filter('code =',code).fetch(1)

            replay.content ='**'+contenttext+'转发：'.decode('utf-8')+recontent[0].content.replace('转发：'.decode('utf-8'),'').replace('**','')+'**'
            logging.info(recontent[0].content.replace('转发：'.decode('utf-8'),'').replace('**',''))
            replay.replyType = '9'
            replay.status = '1'
            replay.level='101'
            isreplay=True

            realCode=ReplyContent().all().filter('code =',code).fetch(1)
            pam['weiboid']=realCode[0].key().name()[1:]
            pam['username']=username
            pam['code']=replay.code
            pam['text']=contenttext.encode('utf-8')
            start2=-1
            start3=-1
            start1=recontent[0].content.find('转发 '.decode('utf-8'))
            if start1!=-1:
                start2=recontent[0].content[start1:].find('转发 '.decode('utf-8'))
                if start2!=-1:
                    start3=recontent[0].content[start2:].find('转发 '.decode('utf-8'))
            if start3==-1:
                start=start2
                if start2==-1:
                    start=start1
                    if start1==-1:
                        start=0
                        recontent[0].content='转发 '.decode('utf-8')+recontent[0].content

            else:
                start=start3
            pam['alltext']=recontent[0].content[start:].encode('utf-8')
            imgids=re.findall('\[\*[A-Za-z]+/(\d+)\*\]',pam['alltext'])
            if imgids:
                pam['img']=imgids[0]
            url= setting.weiboSite+ '/putotherUserWeibo'
            pass
        if '2'==dotype:#评论微博
            realCode=ReplyContent().all().filter('code =',code).fetch(1)
            pam['weiboid']=realCode[0].key().name()[1:]
            pam['username']=username
            pam['text']=contenttext.encode('utf-8')
            url= setting.weiboSite+ '/putComment'
            pass
    if '11'==replaytype:
        if '1'==dotype:#添加关注
            reply=ReplyContent().all().filter('code =',code).fetch(1)
            try:
                pam['userid']=reply[0].key().name().split('+')[1]
            except :
                raise
            pam['username']=username
            url= setting.weiboSite+ '/addUserWeibo'
            pass
        if '2'==dotype:#转发微博
            lastre=ReplyContent().all().filter('code >','a3-0'+username+'-s'+username).filter('code <','a3-0'+username+'-s'+username+u'\ufffd').order('-code').fetch(1)
            if lastre:
                weiboweb=lastre[0].code.split('-')[-1]
            else:
                weiboweb='11'
#            lastcode=int(weiboweb[:-1])+1
            lastcode=str(int(weiboweb[:-1])+1)+weiboweb[-1]
            replay=ReplyContent()
            replay.maincode = 'a3'#主编号
            replay.code = 'a3-0'+username+'-s'+username+'-'+lastcode#编号
            replay.father = 'a3-0'+username+'-s'+username#父级
            recontent=ReplyContent().all().filter('code =',code).fetch(1)

            replay.content ='**'+contenttext+'转发：'.decode('utf-8')+recontent[0].content.replace('转发：'.decode('utf-8'),'').replace('**','')+'**'
            logging.info(recontent[0].content.replace('转发：'.decode('utf-8'),'').replace('**',''))
#            replay.content ='**'+contenttext+'**'
            replay.replyType = '9'
            replay.status = '1'
            replay.level='101'
            isreplay=True

            realCode=ReplyContent().all().filter('code =',code).fetch(1)
            pam['weiboid']=realCode[0].key().name().split('+')[0][2:]
#            pam['weiboid']=code.split('-')[-1]
            pam['username']=username
            pam['code']=replay.code
            pam['text']=contenttext.encode('utf-8')
            start2=-1
            start3=-1
            start1=recontent[0].content.find('转发 '.decode('utf-8'))
            if start1!=-1:
                start2=recontent[0].content[start1:].find('转发 '.decode('utf-8'))
                if start2!=-1:
                    start3=recontent[0].content[start2:].find('转发 '.decode('utf-8'))
            if start3==-1:
                start=start2
                if start2==-1:
                    start=start1
                    if start1==-1:
                        start=0
                        recontent[0].content='转发 '.decode('utf-8')+recontent[0].content
            else:
                start=start3
            pam['alltext']=recontent[0].content[start:].encode('utf-8')
            imgids=re.findall('\[\*[A-Za-z]+/(\d+)\*\]',pam['alltext'])
            if imgids:
                pam['img']=imgids[0]
            url= setting.weiboSite+ '/putotherUserWeibo'
            pass
        if '3'==dotype:#评论微博
            realCode=ReplyContent().all().filter('code =',code).fetch(1)
            pam['weiboid']=realCode[0].key().name().split('+')[0][2:]
#            pam['weiboid']=code.split('-')[-1]
            pam['username']=username
            pam['text']=contenttext.encode('utf-8')
            url= setting.weiboSite+ '/putComment'
            pass
    if '12'==replaytype:
        pass
    try:
        if isGet:
            result = urlfetch.fetch(
                    url =url,
                    payload = {},
                    method = urlfetch.GET,
                    headers = {'Content-Type':'application/x-www-form-urlencoded',
                               'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6'},
                    follow_redirects = False,deadline=10)
        else:
            result = urlfetch.fetch(
                    url =url,
                    payload = urllib.urlencode(pam),
                    method = urlfetch.POST,
                    headers = {'Content-Type':'application/x-www-form-urlencoded',
                               'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6'},
                    follow_redirects = False,deadline=10)
    except Exception,e:
        logging.info(str(e))
        raise e
    if result.status_code==200:
        if isreplay:
            replay.put()
        if delReplay:
            db.put(delReplay)
        self.response.out.write('0')
    else:
        self.response.out.write('1')



def doShouQuan(self):
    userName = self.request.get('UserName')
    content=self.request.get('Content').replace('*','')
    if '1'==content:
        content='sina'
    if '2'==content:
        content='sohu'
    if '3'==content:
        content='wy'
    if '4'==content:
        content='teng'
    weiboweb=memcache.Client().get('weiboweb')
    if not weiboweb:
        setting=Setting().all().fetch(1)
        if not setting:
            self.response.out.write('1')
            return
        setting=setting[0]
        result = urlfetch.fetch(
                    url =setting.weiboSite+'/getUrl',
                    payload = {},
                    method = urlfetch.GET,
                    headers = {'Content-Type':'application/x-www-form-urlencoded',
                               'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6'},
                    follow_redirects = False,deadline=10)
        if result.status_code==200:
            weiboweb=result.content
            memcache.Client().set('weiboweb',weiboweb,360000)
        else:
            weiboweb='/'
    self.redirect(weiboweb+'?username='+userName+'&website='+content)


class WeiboCheck(PublicPage):
    def post(self, *args):
        usernamelist=self.request.get('username').split(',')
        do=self.request.get('do')
        web=self.request.get('web')
        for username in usernamelist:
            for r in ReplyContent().all().filter('code =','a999-2-s1-'+username).fetch(1):
                if r.content.find('*;*')!=-1:
                    contentlist=r.content.split('*;*')
                else:
                    contentlist=r.content.split('**')
                dolist=contentlist[0].split('*')
                notdolist=contentlist[1].split('*')
                while '' in dolist:
                    dolist.remove('')
                while '' in notdolist:
                    notdolist.remove('')
                if not do:
                    l=dolist
                    dolist=notdolist
                    notdolist=l
                if 'sina'==web:
                    if '1' in dolist:
                        dolist.remove('1')
                    if '1' not in notdolist:
                        notdolist.append('1')
                if 'sohu'==web:
                    if '2' in dolist:
                        dolist.remove('2')
                    if '2' not in notdolist:
                        notdolist.append('2')
                if 'wy'==web:
                    if '3' in dolist:
                        dolist.remove('3')
                    if '3' not in notdolist:
                        notdolist.append('3')
                if 'teng'==web:
                    if '4' in dolist:
                        dolist.remove('4')
                    if '4' not in notdolist:
                        notdolist.append('4')
                if not do:
                    l=dolist
                    dolist=notdolist
                    notdolist=l
                r.content='*'+'*'.join(dolist)+'*;*'+'*'.join(notdolist)+'*'
                r.content=r.content
                r.put()




